* This is the do file to create "Table C3. Heterogeneity among Baseline Abilites (Matched Sample)"
set seed 123

use "$path_data/temp/followup_student_parents_matched", clear


gen gend = q1d - 1

local controls DT_score_pre_std DT_time_pre rosen_pre_std cpcs_pre_std i.grade gend branch1 branch2 branch3 income_source1 income_source2 income_source3 income_source4 last_income_per_member hhmember hhheadage hhheadeduyear phone_survey age_tchr
teffects psmatch (followup_cog_std) (treatment `controls') 
psmatch2 treatment `controls', outcome(followup_cog_std) noreplacement
gen psmattrition = 1 if _support!=1
recode psmattrition (.=0)
keep if psmattrition == 0

egen DT_score_pre_std_med = median(DT_score_pre_std)
gen DT_score_pre_std_upper50 = 1 if DT_score_pre_std>DT_score_pre_std_med
recode DT_score_pre_std_upper50 (.=0)

egen rosen_pre_std_med = median(rosen_pre_std)
gen rosen_pre_std_upper50 = 1 if rosen_pre_std>rosen_pre_std_med
recode rosen_pre_std_upper50 (.=0)
rename rosen_pre_std_upper50 RSES_std_upper50

egen cpcs_pre_std_med = median(cpcs_pre_std)
gen cpcs_pre_std_upper50 = 1 if cpcs_pre_std>cpcs_pre_std_med
recode cpcs_pre_std_upper50 (.=0)
rename cpcs_pre_std_upper50 CPCS_std_upper50


/// Cognitive
wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 1, cluster(school_no) reps(1000)
	
scalar n_cog_u = e(N)
scalar n_clust_cog_u = e(N_clust)
matrix r2_followup_cog_std_temp = r(table)


foreach s in mean se pv obs {
		matrix r2_followup_cog_std_upper_`s' = J(1,2,.)
}

foreach j in 1 2 {
* beta
matrix r2_followup_cog_std_upper_mean[1,`j'] = r2_followup_cog_std_temp[1,`j']
* standard error
matrix r2_followup_cog_std_upper_se[1,`j'] = r2_followup_cog_std_temp[2,`j']
* p value
matrix r2_followup_cog_std_upper_pv[1,`j'] = r2_followup_cog_std_temp[3,`j']
}



wildbootstrap reg followup_cog_std treatment if DT_score_pre_std_upper50 == 0, cluster(school_no) reps(1000)

scalar n_cog_l = e(N)
scalar n_clust_cog_l = e(N_clust)
matrix r2_followup_cog_std_temp = r(table)


foreach s in mean se pv obs {
		matrix r2_followup_cog_std_lower_`s' = J(1,2,.)
}

foreach j in 1 2 {
* beta
matrix r2_followup_cog_std_lower_mean[1,`j'] = r2_followup_cog_std_temp[1,`j']
* standard error
matrix r2_followup_cog_std_lower_se[1,`j'] = r2_followup_cog_std_temp[2,`j']
* p value
matrix r2_followup_cog_std_lower_pv[1,`j'] = r2_followup_cog_std_temp[3,`j']
}

wildbootstrap reg followup_cog_std i.treatment##i.DT_score_pre_std_upper50, cluster(school_no) reps(1000)
matrix cog_difference = r(table)
    
/// Non cognitive


foreach dep in RSES_std CPCS_std{
    
    wildbootstrap reg `dep' treatment if `dep'_upper50 == 1, cluster(school_no) reps(1000)
	
	scalar n_`dep'_u = e(N)
	scalar n_clust_`dep'_u = e(N_clust)
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_upper_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_upper_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	matrix r2_`dep'_upper_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_upper_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}


	wildbootstrap reg `dep' treatment if `dep'_upper50 == 0, cluster(school_no) reps(1000)
	
	scalar n_`dep'_l = e(N)
	scalar n_clust_`dep'_l = e(N_clust)
	matrix r2_`dep'_temp = r(table)


    foreach s in mean se pv obs {
		matrix r2_`dep'_lower_`s' = J(1,2,.)
	}

	foreach j in 1 2 {
	* beta
	matrix r2_`dep'_lower_mean[1,`j'] = r2_`dep'_temp[1,`j']
	* standard error
	matrix r2_`dep'_lower_se[1,`j'] = r2_`dep'_temp[2,`j']
	* p value
	matrix r2_`dep'_lower_pv[1,`j'] = r2_`dep'_temp[3,`j']
	}

}

wildbootstrap reg RSES_std i.treatment##i.RSES_std_upper50, cluster(school_no) reps(1000)
matrix RSES_difference = r(table)

wildbootstrap reg CPCS_std i.treatment##i.CPCS_std_upper50, cluster(school_no) reps(1000)
matrix CPCS_difference = r(table)

// significant level

local outcome followup_cog_std RSES_std CPCS_std
local hetero upper lower

foreach dep in `outcome'{
foreach h in `hetero'{
		if r2_`dep'_`h'_pv[1,1]<=0.01 {
			local star_`dep'_`h' %3s "***"
		}
		else if (r2_`dep'_`h'_pv[1,1]>0.01) & (r2_`dep'_`h'_pv[1,1]<=0.05) {
			local star_`dep'_`h' %2s "**"
		}
		else if (r2_`dep'_`h'_pv[1,1]>0.05) & (r2_`dep'_`h'_pv[1,1]<=0.10) {
			local star_`dep'_`h' %1s "*"
		}
		else {
			local star_`dep'_`h'  ""
		}
} 
}



/// Table
tempname hh2
file open `hh2' using "$path_output/hetero_matchsample.tex", write replace
file write `hh2' "" _newline
file write `hh2' "% Author: Kazuma Takakura" _newline
file write `hh2' "% Date: `c(current_date)'" _newline
file write `hh2' "% Time: `c(current_time)'" _newline
file write `hh2' "" _newline


file write `hh2' "\begin{table}[h!]\footnotesize" _newline
file write `hh2' "  \centering" _newline
file write `hh2' "  \caption{Heterogeneity among Baseline Abilites (Matched Sample)}" _newline
file write `hh2' "\label{tab:hetero_match}" _newline
file write `hh2' "\scalebox{1}{" _newline
file write `hh2' "\begin{threeparttable}" _newline

file write `hh2' "\begin{tabular}{lccc}\toprule" _newline

file write `hh2' "  & Top 50\%^{b}  & Bottom 50\%^{b} & Differences  \\\midrule" _newline
file write `hh2' "\multicolumn{4}{c}{Panel A: Rapid math test score^{a}}\\\midrule" _newline
file write `hh2' " Treatment & " %04.3f (r2_followup_cog_std_upper_mean[1,1]) `star_followup_cog_std_upper' "  & " %04.3f (r2_followup_cog_std_lower_mean[1,1]) `star_followup_cog_std_lower' " &  " %04.3f (cog_difference[1,3]) " \\" _newline
file write `hh2' " & ( " %04.3f (r2_followup_cog_std_upper_pv[1,1]) " ) & ( " %04.3f (r2_followup_cog_std_lower_pv[1,1]) " ) & ( " %04.3f (cog_difference[3,3]) " ) \\ " _newline
file write `hh2' " Observation &  " %02.0f ( n_cog_u ) " & " %02.0f ( n_cog_l ) " &  \\ " _newline
file write `hh2' " N of clusters &  " %02.0f ( n_clust_cog_u ) " & " %02.0f ( n_clust_cog_l ) " &  \\\midrule " _newline

file write `hh2' "\multicolumn{4}{c}{Panel B: RSES score^{a}}\\\midrule" _newline
file write `hh2' " Treatment & " %04.3f (r2_RSES_std_upper_mean[1,1]) `star_RSES_std_upper' "  & " %04.3f (r2_RSES_std_lower_mean[1,1]) `star_RSES_std_lower' " &  " %04.3f (RSES_difference[1,3]) " *** \\" _newline
file write `hh2' " & ( " %04.3f (r2_RSES_std_upper_pv[1,1]) " ) & ( " %04.3f (r2_RSES_std_lower_pv[1,1]) " ) & ( " %04.3f (RSES_difference[3,3]) " ) \\ " _newline
file write `hh2' " Observation &  " %02.0f ( n_RSES_std_u ) " & " %02.0f ( n_RSES_std_l ) " &  \\ " _newline
file write `hh2' " N of clusters &  " %02.0f ( n_clust_RSES_std_u ) " & " %02.0f ( n_clust_RSES_std_l ) " &  \\\midrule " _newline

file write `hh2' "\multicolumn{4}{c}{Panel C: CPCS score^{a}}\\\midrule" _newline
file write `hh2' " Treatment & " %04.3f (r2_CPCS_std_upper_mean[1,1]) `star_CPCS_std_upper' "  & " %04.3f (r2_CPCS_std_lower_mean[1,1]) `star_CPCS_std_lower' " &  " %04.3f (CPCS_difference[1,3]) " \\" _newline
file write `hh2' " & ( " %04.3f (r2_CPCS_std_upper_pv[1,1]) " ) & ( " %04.3f (r2_CPCS_std_lower_pv[1,1]) " ) & ( " %04.3f (CPCS_difference[3,3]) " ) \\ " _newline
file write `hh2' " Observation &  " %02.0f ( n_CPCS_std_u ) " & " %02.0f ( n_CPCS_std_l ) " &  \\ " _newline
file write `hh2' " N of clusters &  " %02.0f ( n_clust_CPCS_std_u ) " & " %02.0f ( n_clust_CPCS_std_l ) " &  \\\midrule " _newline

file write `hh2' "\end{tabular}" _newline
file write `hh2' "\begin{tablenotes}" _newline
file write `hh2' "\item (a) Dependent variables are standardized using the average and variance of the whole follow-up sample in the February 2022 survey." _newline
file write `hh2' "\item (b) Cutoffs are created based on whether their ability to perform each item at the baseline was higher or lower than the median. " _newline
file write `hh2' "\item (c) Wild cluster bootstrap p-values are reported within parentheses. Clusters are schools at the baseline. " _newline
file write `hh2' "\item (d) $^*$ Significant at 10\% level; $^{**}$ significant at 5\% level; $^{***}$ significant at 1\% level. " _newline
file write `hh2' "\end{tablenotes}" _newline
file write `hh2' "\end{threeparttable}" _newline
file write `hh2' "}" _newline
file write `hh2' "\label{tab:addlabel}%" _newline
file write `hh2' "\end{table}" _newline

file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline
file write `hh2' "" _newline

file close `hh2'
